articles

Home / DeveloperSection / Articles / INTRODUCTION TO OOPS

INTRODUCTION TO OOPS

NAMAN PANDEY 2564 19-Jun-2018

                                                                                                                                                          OBJECT-ORIENTED PROGRAMMING (OOP)

OOP:

OOP is basically a programming style that involves the concept of objects and class.We can also say that OOP is an approach to a problem involving objects. Our objective is to tell a set of instructions to a computer with the help of programming language. To achieve this there are various ways of doing it and OOP is one of those ways.

There are various concepts under OOPs which are;

1) Objects

2) Classes

3) Inheritance

4) Data encapsulation

5) Data abstraction

6) Polymorphism

We will discuss these concepts in details,

1) OBJECTS:

Objects are the basic unit of OOPs. Objects are basically instances of a class. We can say that class as the description of a concept, and an object as the realization of this description to create an independent distinguishable entity.

Objects can be concrete or conceptual each with its own individual identity.

These real-world objects share two characteristics: they all have state and they all have behavior. For example, we humans have hands, legs, eyes, nose, ears these are the data and walking, reading, writing, sleeping, eating are the methods.

Objects are made after real-world objects. A software object maintains its data in variables and implements the methods.

2) CLASSES:

Classes are basically a blueprint of the functional entity which defines its properties and function. Classes contain both data and functions. A class can have the subclass which can inherit data/properties of that class.

The above diagrams show an exact meaning of classes.

3) INHERITANCE:

Inheritance is a process in which properties of parent class are inherited by the child class. For example, if there is a parent class and a child class and we declare a variable or method in the parent class we can use that in child class without declaring that variable or method again.

There following are the types of inheritance:

1) SINGLE INHERITANCE

Above diagram shows the concept of single inheritance.

In single inheritance base class derive the properties from only one parent class.

Syntax:

class subclass_name : access_mode base_class
{
  //body of the subclass
};

EXAMPLE:

// C++ program to explain
// Single inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
// sub class derived from two base classes
class Car: public Vehicle{
};
// main function
int main()
{
    // creating object of subclass will
    // invoke the constructor of base classes
    Car obj;
    return 0;
}
Output:
This is a vehicle

2) MULTIPLE INHERITANCE

In multiple inheritance properties of sub/child class are derived from two super/parent classes.

EXAMPLE:

// C++ program to explain
// multiple inheritance
#include <iostream>
using namespace std;
// first base class
class Vehicle {
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
// second base class
class FourWheeler {
  public:
    FourWheeler()
    {
      cout << "This is a 4 wheeler Vehicle" << endl;
    }
};
// sub class derived from two base classes
class Car: public Vehicle, public FourWheeler {
};
// main function
int main()
{
    // creating object of sub class will
    // invoke the constructor of base classes
    Car obj;
    return 0;
}
Output:
This is a Vehicle
This is a 4 wheeler Vehicle

3) MULTILEVEL INHERITANCE:

In multilevel inheritance, a derived class is derived from another base class.

EXAMPLE:

// C++ program to implement
// Multilevel Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
class fourWheeler: public Vehicle
{ public:
    fourWheeler()
    {
      cout<<"Objects with 4 wheels are vehicles"<<endl;
    }
};
// sub class derived from two base classes
class Car: public fourWheeler{
   public:
     car()
     {
       cout<<"Car has 4 Wheels"<<endl;
     }
};
// main function
int main()
{
    //creating object of the subclass will
    //invoke the constructor of base classes
    Car obj;
    return 0;
}
output:
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels

4) HIERARCHICAL INHERITANCE:

In hierarchical inheritance, more than one subclass is inherited from a single  base class. i.e. more than one derived class is created from a single base class.

EXAMPLE:

// C++ program to implement
// Hierarchical Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
// first sub class
class Car: public Vehicle
{
};
// second sub class
class Bus: public Vehicle
{
};
// main function
int main()
{
    // creating object of sub class will
    // invoke the constructor of base class
    Car obj1;
    Bus obj2;
    return 0;
}
Output:
This is a Vehicle
This is a Vehicle

5) HYBRID INHERITANCE:

Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.

EXAMPLE:

// C++ program for Hybrid Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
//base class
class Fare
{
    public:
    Fare()
    {
        cout<<"Fare of Vehicle\n";
    }
};
// first sub class
class Car: public Vehicle
{
};
// second sub class
class Bus: public Vehicle, public Fare
{
};
// main function
int main()
{
    // creating object of sub class will
    // invoke the constructor of base class
    Bus obj2;
    return 0;
}
Output:
This is a Vehicle
Fare of Vehicle

Syntax:

class subclass_name : access_mode base_class
{
  //body of subclass
};

EXAMPLE:

// C++ program to explain
// Single inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
// sub class derived from two base classes
class Car: public Vehicle{
};
// main function
int main()
{
    // creating object of subclass will
    // invoke the constructor of base classes
    Car obj;
    return 0;
}
Output:
This is a vehicle

4) DATA ABSTRACTION:

Data abstraction means showcasing only the required things to the outside world while hiding the details. Basically, data abstraction is done to hide the complexity of the program.

Example: When we drive a car and press the accelerator to increase the speed or press the brakes to stop the car even without knowing how the accelerator or the brakes work as the driver is not concerned with the working. This is an example of data abstraction as here we hide the working and providing only the essential part to the user.

Data abstraction helps in various ways such as:

1) Prevent from writing low-level code

2) avoid code duplication and increases reusability.

3) Helps to increase security.

5) DATA ENCAPSULATION:

The process of hiding the implementation details of the class from the user is called as data encapsulation. This an achieved through the private fields and the public methods of classes. Data abstraction is basically bound data variables and functions together in a class.

Data encapsulation is also known as data hiding. Data encapsulation can be achieved by access specifiers which are public, protected, and private. These specifiers help in hiding the data and exposing those only which need to be exposed.

EXAMPLE:

#include <iostream>
using namespace std;
class Adder {
   public:
      // constructor
      Adder(int i = 0) {
         total = i;
      }
      // interface to outside world
      void addNum(int number) {
         total += number;
      }
      // interface to outside world
      int getTotal() {
         return total;
      };
   private:
      // hidden data from outside world
      int total;
};
int main() {
   Adder a;
   a.addNum(10);
   a.addNum(20);
   a.addNum(30);
   cout << "Total " << a.getTotal() <<endl;
   return 0;
}
When the above code is compiled and executed, it produces the following result −
Total 60

Above class adds numbers together, and returns the sum. The public members addNum and getTotal are the interfaces to the outside world and a user needs to know them to use the class. The private member total is something that is hidden from the outside world but is needed for the class to operate properly.

6) POLYMORPHISM:

The word polymorphism means having many forms. Polymorphism is a concept which allows us to redefine the way something works, by either changing how it is done or by changing the parts using which it is done.

It is a feature which let us create functions with same name but different arguments, which will perform different actions.

Polymorphism can be static as well as dynamic both. Method overloading is static polymorphism whereas method overriding is the dynamic polymorphism.

Overloading means that when the method name is same but have different arguments. It is also called as compile-time polymorphism or early binding.

Some rules regarding method overloading are:

1) The method name must be same.

2) Method argument must be different.

3) Method signature must be different.

Overriding means to redefine a function. The method written in the parent class can be redefined in the child class. It is also called runtime polymorphism or late binding.

Some rules regarding method overriding are:

1) The method name must be same

2) Method argument must be same including the order

3) Method signature must be same

ADVANTAGES OF OOPS:

1) Objects created in object-oriented programs can be used in other programs hence it helps in code reusability.

2) It helps in data redundancy.

3) It helps in data security.

4) It helps in data hiding.

5) It helps in code maintenance.



Updated 03-Mar-2019

Leave Comment

Comments

Liked By